//
//  TraktApi.swift
//  Checked
//
//  Created by admin on 01/03/2017.
//  Copyright © 2017 shinolr_go. All rights reserved.
//

import UIKit
import Moya

let clientID = "0d1813849638e4ae85af190eb3c0c7899f2d296d5852c2b8f6d50efdf2290332"
let clientSecret = "e8289f3b83d9feb647325105a87019b70356afebad4dc62544ca4a861afaf443"
let redirect_uri = "checkedapp://oauth"

let traktProvider = MoyaProvider<TraktApi>()

enum TraktApi {
	case authorize
	case diveceCode
	case accessToken(code: String)
	case myShows
	case allShows
    case watched // 观看 top 10
}

extension TraktApi: TargetType {

    var baseURL: URL {
        switch self {
        case .authorize:
            return URL(string: "https://trakt.tv")!
        case .diveceCode:
            return URL(string: "https://trakt.tv")!
        case .accessToken:
            return URL(string: "https://api.trakt.tv")!
        case .myShows:
            return URL(string: "https://api.trakt.tv")!
        case .allShows:
            return URL(string: "https://api.trakt.tv")!
        case .watched:
            return URL(string: "https://api.trakt.tv")!
        }
    }

    var path: String {
        switch self {
        case .authorize:
            return "/oauth/authorize"
        case .diveceCode:
            return "/oauth/device/code"
        case .accessToken:
            return "/oauth/token"
        case .myShows:
            return "/calendars/my/shows"
        case .allShows:
            return "/calendars/all/shows"
        case .watched:
            return "/shows/watched"
        }
    }

    var method: Moya.Method {
        switch self {
        case .authorize:
            return .get
        case .diveceCode:
            return .post
        case .accessToken:
            return .post
        case .myShows:
            return .get
        case .allShows:
            return .get
        case .watched:
            return .get
        }
    }

    var sampleData: Data {
//        switch self {
//        case .accessToken(code: _):
//            return "{\"id\": \"id\", \"first_name\": \"Harry\", \"last_name\": \"Potter\"}".data(using: String.Encoding.utf8)!
//        default:
//            return "Half measures are as bad as nothing at all.".data(using: String.Encoding.utf8)!
//        }
        return Data()
    }

    var task: Task {
        switch self {
        case .authorize:
            return .requestParameters(parameters: ["response_type": "code",
                                                       "client_id": clientID,
                                                    "redirect_uri": redirect_uri],
                                        encoding: URLEncoding.default)
        case .diveceCode:
            return .requestPlain
        case .accessToken(let code):
            return .requestParameters(parameters: ["code": code,
                                              "client_id": clientID,
                                          "client_secret": clientSecret,
                                           "redirect_uri": redirect_uri,
                                             "grant_type": "authorization_code"],
                                        encoding: URLEncoding.default)
        case .myShows:
            return .requestParameters(parameters: ["start_date": "2019-01-01",
                                                         "days": 7],
                                        encoding: URLEncoding.default)
        case .allShows:
            return .requestParameters(parameters: [
//                "start_date": "2019-01-01",
                                                         "days": 1],
                                        encoding: URLEncoding.default)
        case .watched:
            return .requestParameters(parameters: ["period": "weekly"], encoding: URLEncoding.default)
        }
    }

    var headers: [String: String]? {
        switch self {
        case .myShows:
            return ["Content-Type": "application/json",
                   "Authorization": "Bearer \(AccessTokenModel.currentAccessToken!)",
               "trakt-api-version": "2",
                   "trakt-api-key": "\(clientID)"]
        case .allShows:
            return ["Content-Type": "application/json",
                   "Authorization": "Bearer \(AccessTokenModel.currentAccessToken!)",
               "trakt-api-version": "2",
                   "trakt-api-key": "\(clientID)"]
        case .watched:
            return ["Content-Type": "application/json",
                   "Authorization": "Bearer \(AccessTokenModel.currentAccessToken!)",
               "trakt-api-version": "2",
                   "trakt-api-key": "\(clientID)"]
        default:
            return nil
        }
    }

//
//    var parameters: [String: Any]? {
//        switch self {
//        case .authorize:
//            return ["response_type": "code",
//                        "client_id": clientID,
//                     "redirect_uri": redirect_uri]
//        case .diveceCode:
//            return ["client_id": clientID]
//        case let .accessToken(code):
//            return          ["code": code,
//                        "client_id": clientID,
//                    "client_secret": clientSecret,
//                     "redirect_uri": redirect_uri,
//                       "grant_type": "authorization_code"]
//        case .myShows:
//            return ["start_date": Date().dashTime,
//                    "days": days]
//        case .allShows:
//            return ["start_date": Date().dashTime,
//                    "days": days]
//        }
//
//    }
//    var parameterEncoding: ParameterEncoding {
//        //        switch self {
//        //        case .authorize:
//        return URLEncoding.default // Send parameters in URL
//        //        }
//    }
//    var sampleData: Data {
//        //        switch self {
//        //        case .authorize:
//        return "Half measures are as bad as nothing at all.".data(using: String.Encoding.utf8)!
//        //        }
//    }
//    var task: Task {
//        //        switch self {
//        //        case .authorize:
//        return .request
//        //        }
//    }
}

extension Moya.Response {
    func mapNSArray() throws -> NSArray {
        let any = try self.mapJSON()
        guard let array = any as? NSArray else {
            throw MoyaError.jsonMapping(self)
        }
        return array
    }
}
